package Network;

import chataudio.Constant;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
 *
 * @author Matthieu
 */
public class ServeurUDP {

    //final static int PORT_SERVEUR = 2510;
    final static int TAILLE_MAX_BUFFER=650;
    
    int port = 2510;
    //DatagramSocket ds1 = null;
    DatagramPacket inPacket = null;        
    byte[] bufferin = new byte[TAILLE_MAX_BUFFER];

            DatagramSocket ds =  new DatagramSocket(Constant.udpPort);
    
    // Creation de la socket en ecoute sur le port passé en parametres
    public ServeurUDP(int num_port) throws SocketException
    {
        try{
            inPacket = new DatagramPacket(bufferin, bufferin.length);
            //ds = new DatagramSocket(num_port);
            System.out.println(num_port);
            System.out.println(ds.getPort());
        }
        catch(Exception e){
           // ds = new DatagramSocket(2511);
        }
    }
    
    // Creation de la socket en ecoute sur le port par default
    public ServeurUDP() throws SocketException{
       // ds = new DatagramSocket(PORT_SERVEUR);
    }       
    
    
    // Ecoute sur le port et retourne le tableau d'octets recu
    public byte[] Listen() throws IOException
    {
            //datagramSocket = new DatagramSocket(2500);
        		       
        System.out.println(" listen UDP port: " + ds.getPort() );
        ds.receive(inPacket);
        System.out.println("adress: " + inPacket.getAddress());
        System.out.print(" out listen UDP ");
        
        for ( int i=0; i<10; i++)
       System.out.println((char)inPacket.getData()[i]);
        
        return inPacket.getData();
    }
}
